package com.zjitc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.sendphone.SendPhone;
import com.zjitc.dao.UserDao;
import com.zjitc.model.Shipping;
import com.zjitc.model.User;
import com.zjitc.service.ShippingService;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/24 0024
 * @description:
 */
@Controller
public class ShippingControl {

    @Autowired
    private ShippingService shippingService;
    @Autowired
    private Jedis jedis;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/sendMessage")
    public void sendMessage(String telephone, HttpSession session) throws ClientException {
        SendPhone.sendSms(telephone, session);
        System.out.println(telephone);
    }

    @RequestMapping(value = "/addAddress")
    @ResponseBody
    public String addAddress(
            @RequestParam(name = "address") String address,
            @RequestParam(name = "consignee") String consignee,
            @RequestParam(name = "telephone") String telephone,
            @RequestParam(name = "code") String code, HttpSession session) {
        User one = (User) session.getAttribute("user");
        String s = (String) session.getAttribute(one.getUid() + "code");
        if (s.equals(code)) {
            Shipping shipping = new Shipping();
            User user = (User) session.getAttribute("user");
            shipping.setUid(user.getUid());
            shipping.setShippingAddress(address);
            shipping.setShippingName(consignee);
            shipping.setShippingTelephone(telephone);
            shippingService.add(shipping);
            user.setTelephone(telephone);
            userService.update(user);
            return Integer.toString(1);
        } else {
            return Integer.toString(-1);
        }
    }

    @RequestMapping(value = "/updateAddress")
    public void updateAddress(
            @RequestParam(name = "address") String address,
            @RequestParam(name = "consignee") String consignee,
            @RequestParam(name = "telephone") String telephone, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Shipping shipping = shippingService.findOne(user.getUid());
        if (shipping != null) {
            shipping.setSid(shipping.getSid());
            shipping.setShippingAddress(address);
            shipping.setShippingName(consignee);
            shipping.setShippingTelephone(telephone);
            shippingService.update(shipping);
        }
    }

    @RequestMapping(value = "/findOne")
    @ResponseBody
    public Shipping findOne(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Shipping shipping = shippingService.findOne(user.getUid());
        return shipping;
    }
}
